package com.hmall.es;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.item.ItemApplication;
import com.hmall.item.domain.po.Item;
import com.hmall.item.domain.po.ItemDoc;
import com.hmall.item.service.IItemService;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

@SpringBootTest(properties = "spring.profiles.active=local",classes = ItemApplication.class)
public class esDocTestClass {
    // 进行初始化
    private RestHighLevelClient client;
    @Autowired
    private IItemService iItemService;
    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.100.100:9200")
        ));
    }
    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }

    // 增（全量修改）
    @Test
    public void testCreateIndexDoc() throws IOException {
        Item byId = iItemService.getById(317578);
        ItemDoc itemDoc = BeanUtil.copyProperties(byId, ItemDoc.class);
        // 请求
        IndexRequest item = new IndexRequest("item").id(itemDoc.getId());
        // 请求体
        item.source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);
        // 请求参数
        client.index(item, RequestOptions.DEFAULT);
    }

    //局部的修改
    @Test
    public void testPostDoc() throws IOException {
        // 请求
        UpdateRequest request = new UpdateRequest("item","317578");
        // 请求体
        request.doc(
                "price", 66666
        );
        // 请求参数
        client.update(request, RequestOptions.DEFAULT);
    }

    // 删
    @Test
    public void testDelete() throws IOException {
        // 创建请求
        DeleteRequest item = new DeleteRequest("item", "317578");
        // 发送请求
        client.delete(item, RequestOptions.DEFAULT);
    }
    // 查
    @Test
    public void testQueryIndex() throws IOException {
        // 创建请求
        GetRequest item = new GetRequest("item", "317578");
        // 发送请求
        GetResponse re = client.get(item, RequestOptions.DEFAULT);
        String sourceAsString = re.getSourceAsString();
        ItemDoc bean = JSONUtil.toBean(sourceAsString, ItemDoc.class);
        System.out.println("doc:"+bean);
    }

    // 批量查询
    @Test
    public void testBatchQueryIndex() throws IOException {
        int pageNo = 1;
        while (true) {
            // 查数据
            Page<Item> page = iItemService.lambdaQuery()
                    .eq(Item::getStatus, 1)
                    .page(Page.of(pageNo, 500));
            List<Item> records = page.getRecords();
            if (records == null|| records.isEmpty()){
                return;
            }

            // 获取批处理的对象
            BulkRequest request = new BulkRequest();
            // 参数传
            for (Item item : records){
                request.add(new IndexRequest("item")
                        .id(item.getId().toString())
                        .source(JSONUtil.toJsonStr(BeanUtil.copyProperties(item,ItemDoc.class)),XContentType.JSON));
            }
            // 请求
            client.bulk(request, RequestOptions.DEFAULT);
            pageNo++;
        }
    }

}
